package com.wzdigit.wms.wms.controller.TransferWareHouse;


import com.wzdigit.framework.exception.RestStatusException;
import com.wzdigit.wms.basic.domain.basic.Vehicle;
import com.wzdigit.wms.basic.domain.dtos.inventory.SnInforDto;
import com.wzdigit.wms.basic.client.request.asn.AsnRequest;
import com.wzdigit.wms.basic.client.request.transfer.CreateTransferRequest;
import com.wzdigit.wms.basic.client.response.Result;
import com.wzdigit.wms.basic.client.response.basic.WarehouseApiResponse;
import com.wzdigit.wms.wms.constant.WmsWareConstant;
import com.wzdigit.wms.wms.service.mid.IfPostbackService;
import com.wzdigit.wms.wms.service.order.AsnService;
import com.wzdigit.wms.wms.service.order.TurnWarehouseService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 * 转仓_PDA 前端控制器
 * </p>
 *
 * @author
 * @since 2021-01-12
 */
//@RestController
//@RequestMapping(WmsWareConstant.API_VERSION)

@Slf4j
@RestController
@RequestMapping(value = WmsWareConstant.API_VERSION)
@Api(tags = "转仓_PDA")
public class TurnWshController {

    public static final String REQUEST_MODEL = "/turnWsh";

    @Autowired
    AsnService asnService;

    @Autowired
    TurnWarehouseService turnWarehouseService;

    @Autowired
    @Lazy
    IfPostbackService ifPostbackService;


    @Deprecated
    @ApiOperation(value = "生成收货单")
    @PostMapping(value = REQUEST_MODEL + "/addAsn")
    public Result<AsnRequest> addAsn(@RequestBody AsnRequest asnRequest,
                                     HttpServletRequest servletRequest,
                                     HttpServletResponse servletResponse) throws Exception {
        try {
            Result<AsnRequest> result = asnService.addAsn(asnRequest, servletRequest, servletResponse);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @Deprecated
    @ApiOperation(value = "更新收货单")
    @PutMapping(value = REQUEST_MODEL + "/updateAsn")
    public Result updateAsn(@RequestBody AsnRequest asnRequest,
                            HttpServletRequest servletRequest,
                            HttpServletResponse servletResponse) throws Exception {
        try {
            asnService.updateAsn(asnRequest, servletRequest);
            return null;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "获取【转出仓库】下拉信息")
    @GetMapping(value = REQUEST_MODEL + "/getRollOutWarehouse/{userId}")
    public Result<List<WarehouseApiResponse>> getRollOutWarehouse(@PathVariable(value = "userId") @ApiParam("用户id") Integer userId,
                                                                  HttpServletRequest servletRequest,
                                                                  HttpServletResponse servletResponse) throws Exception {
        try {
            Result<List<WarehouseApiResponse>> result = turnWarehouseService.getRollOutWarehouse(userId);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "获取【目的仓库】下拉信息")
    @GetMapping(value = REQUEST_MODEL + "/getTargetWarehouse/{userId}")
    public Result<List<WarehouseApiResponse>> getTargetWarehouse(@PathVariable(value = "userId") @ApiParam("用户id") Integer userId,
                                                                 HttpServletRequest servletRequest,
                                                                 HttpServletResponse servletResponse) throws Exception {
        try {
            Result<List<WarehouseApiResponse>> result = turnWarehouseService.getTargetWarehouse();
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    @ApiOperation(value = "获取车辆信息")
    @GetMapping(value = REQUEST_MODEL + "/getVehicleInfo")
    public Result<List<Vehicle>> getVehicleInfo(@RequestParam(required = false) @ApiParam("查询关键字") String selectKey,
                                                HttpServletRequest servletRequest,
                                                HttpServletResponse servletResponse) throws Exception {
        try {
            Result<List<Vehicle>> result = turnWarehouseService.getVehicleInfo(selectKey);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    /**
     * @param sn
     * @param twScanMode      单台0
     * @param whsId
     * @param servletRequest
     * @param servletResponse
     * @return
     * @throws Exception
     */
    @ApiOperation(value = "扫描产品SN,获取条码信息")
    @GetMapping(value = REQUEST_MODEL + "/getSnInfo/{sn}/{twScanMode}/{whsId}")
    public Result<List<SnInforDto>> getSnInfo(@PathVariable("sn") @ApiParam("产品SN") String sn,
                                              @PathVariable("twScanMode") @ApiParam("扫描方式") Integer twScanMode,
                                              @PathVariable("whsId") @ApiParam("转出仓ID") Integer whsId,
                                              HttpServletRequest servletRequest,
                                              HttpServletResponse servletResponse) throws Exception {
        try {
            Result<List<SnInforDto>> result = turnWarehouseService.getSnInfo(sn, twScanMode, whsId);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }

    //todo 入参：转出仓库，目的仓库，车辆信息，产品条码明细，用户ID
    @ApiOperation(value = "提交创建转仓单")
    @PostMapping(value = REQUEST_MODEL + "/createTransfer")
    public Result<String> createTransfer(@RequestBody CreateTransferRequest createTransferRequest,
                                 HttpServletRequest servletRequest,
                                 HttpServletResponse servletResponse) throws Exception {
        try {
            Result<String> result = turnWarehouseService.createTransferRequest(createTransferRequest, servletRequest);
            return result;
//            throw new RestStatusException("等下哈，这个功能还有点问题");
//            return null;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "返回按钮，返回时写入缓存")
    @PostMapping(value = REQUEST_MODEL + "/backAndInsert")
    public Result backAndInsert(@RequestBody CreateTransferRequest createTransferRequest,
                                HttpServletRequest servletRequest,
                                HttpServletResponse servletResponse) throws Exception {
        try {
            Result result = turnWarehouseService.backAndInsert(createTransferRequest);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @ApiOperation(value = "根据用户ID获取转仓缓存")
    @GetMapping(value = REQUEST_MODEL + "/getTurnCache/{userId}")
    public Result<CreateTransferRequest> getTurnCache(@PathVariable(value = "userId") Integer userId,
                                                      HttpServletRequest servletRequest,
                                                      HttpServletResponse servletResponse) throws Exception {
        try {
            Result<CreateTransferRequest> result = turnWarehouseService.getTurnCache(userId);
            return result;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }


    @Deprecated
    @ApiOperation(value = "test")
    @GetMapping(value = REQUEST_MODEL + "/test")
    public Result<CreateTransferRequest> test(
            HttpServletRequest servletRequest,
            HttpServletResponse servletResponse) throws Exception {
        try {

            ifPostbackService.testAddNew();
            return null;
        } catch (RestStatusException ex) {
            servletResponse.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            throw ex;
        }
    }
}
